<?php
/**
 * 库存log
 * @param $agenId 代理Id
 * @param $sourceId 来源ID
 * @param $change 改变量
 * @param $before 之前
 * @param $info 备注
 */
function stockLog($agenId, $sourceId, $productId, $change, $before, $info = '')
{
    $data = array(
        'sourceid' => $sourceId,
        'agenid' => $agenId,
        'productid' => $productId,
        'numchange' => $change,
        'beforenum' => $before,
        'afternum' => $before + $change,
        'createtime' => now_datetime(),
        'info' => $info
    );
    return M('stockLog')->add($data);
}

/**
 *  积分log
 * @param $reflectId 各表id
 * @param $userId   用户id
 * @param $num  数量
 * @param $type 类型：1：转入 2：转出 3：交易买入 4：交易卖出 5：积分释放
 * @param $before  变更前
 * @return mixed
 */

function integrallog($reflectId, $userId, $num, $type, $before,$after)
{
    $data = array(
        'reflectId' => $reflectId,
        'userId' => $userId,
        'type' => $type,
        'num' => $num,
        'before' => $before,
        'after' => $after,
        'createTime' => now_datetime(),
        'updateTime' => now_datetime(),
    );
    return M('integrallog')->add($data);
}

/**
 * 余额log
 * @param $reflectId 各表id
 * @param $userId   用户id
 * @param $num  数量
 * @param $type 类型
 * @param $before  变更前
 * @return mixed
 */

function balancelog($reflectId, $userId, $num, $type, $before,$after)
{
    $data = array(
        'reflectId' => $reflectId,
        'userId' => $userId,
        'type' => $type,
        'num' => $num,
        'before' => $before,
        'after' => $after,
        'createTime' => now_datetime(),
        'updateTime' => now_datetime(),
    );
    return M('balancelog')->add($data);
}

/**
 * 变更代理金币
 *
 * @param $accountId 账号Id
 * @param $sourceId 来源Id
 * @param $sourceType 来源类型
 * @param $beforeMoney 变更前
 * @param $changeMoney 变更量
 */
function agenMoneyLog($accountId, $sourceId, $sourceType, $beforeMoney, $changeMoney, $info = '')
{

    // 变更类型 1:增加 2:减少
    if ($changeMoney < 0) {
        $type = 2;
    } else {
        $type = 1;
    }

    $data = array(
        'accountId' => $accountId,
        'accountType' => 2,
        'sourceId' => $sourceId,
        'sourceType' => $sourceType,
        'type' => $changeMoney < 0 ? 2 : 1,
        'moneyType' => 2,
        'beforeMoney' => $beforeMoney,
        'changeMoney' => $changeMoney,
        'afterMoney' => $beforeMoney + $changeMoney,
        'createTime' => now_datetime(),
        'info' => $info
    );
    return M('moneyLog')->add($data);
}